﻿using KZ.Express.ENT;
using KZ.Express.Resource;
using KZ.Express.Task;
using KZ.Utilities;
using System;
using System.Data;

namespace KZ.Express.Presenter
{
    public interface IViewBase
    {
        ActionName Action { get; set; }

        SearchCriteria Search { get; }

        string OrderBy { get; }

        string OrderDirection { get; }

        int EntityID { get; set; }

        string DocumentNumber { set; }

        EntityBase Entity { get; set; }

        DataTable Table { get; set; }

        void SetMessage(string message, bool isSuccess);

        void ChangeProgressBarVisible(bool isVisible);

        void ChangeProgressBarMaximum(int value);

        void PerformStepProgressBar(int value);

        void UpdateValueProgressBar(int value);
    }

    public interface IPresenterBase
    {
        void LoadEntity();

        void LoadDataTable();

        void LoadNextSequence(Sequences sequenceID);

        DataTable LoadDataTable<T>(SearchCriteria search);

        DataTable LoadForCMB<T>(SearchCriteria search);

        void Delete(string content);

        void Save();
    }

    public class PresenterBase : IPresenterBase
    {
        protected IViewBase _view;
        protected ITaskBase _task;

        public PresenterBase(IViewBase view)
        {
            _view = view;
            _task = new TaskBase();
        }

        public PresenterBase(IViewBase view, EntityBase entity)
        {
            _view = view;
            _task = new TaskBase(entity);
        }

        public bool IsPermission(int objectID, int permissionID)
        {
            SearchCriteria search = new SearchCriteria();
            search.Add("ObjectID", objectID);
            search.Add("RoleID", UserENT.ROLEID);
            DataTable objects =_task.LoadDataTable<ObjectENT>(search);

            string permission = "," + Utility.StringParse(objects.Rows[0]["PermissionID"]) + ",";
            return permission.Contains("," + permissionID + ",");
        }

        public virtual void LoadEntity()
        {
            try
            {
                _view.Entity = _task.LoadEntity(_view.EntityID);
            }
            catch (DataAccessException ex)
            {
                throw new BusinessLogicException(ex.Message);
            }
        }

        public virtual void LoadDataTable()
        {
            try
            {
                _view.SetMessage(String.Empty, true);
                _view.Table = _task.LoadDataTable(_view.Search);
            }
            catch (DataAccessException ex)
            {
                throw new BusinessLogicException(ex.Message);
            }
        }

        public virtual DataTable LoadDataTable<T>(SearchCriteria search)
        {
            try
            {
                _view.SetMessage(String.Empty, true);
                return _task.LoadDataTable<T>(search ?? new SearchCriteria());
            }
            catch (DataAccessException ex)
            {
                throw new BusinessLogicException(ex.Message);
            }
        }

        public virtual DataTable LoadForCMB<T>(SearchCriteria search)
        {
            try
            {
                _view.SetMessage(String.Empty, true);
                return _task.LoadForCMB<T>(search ?? new SearchCriteria());
            }
            catch (DataAccessException ex)
            {
                throw new BusinessLogicException(ex.Message);
            }
        }

        public virtual void LoadNextSequence(Sequences sequenceID)
        {
            try
            {
                _view.DocumentNumber = _task.LoadNextSequence(sequenceID);
            }
            catch (DataAccessException ex)
            {
                throw new BusinessLogicException(ex.Message);
            }
        }

        public virtual void Delete(string content)
        {
            try
            {
                _task.Delete(content);
                _view.Table = _task.LoadDataTable(_view.Search);
                _view.SetMessage(Message.BLM_SUC_DELETE.ToString(), true);
            }
            catch (DataAccessException ex)
            {
                throw new BusinessLogicException(ex.Message);
            }
        }

        public virtual void Save()
        {
            try
            {
                if (_view.Action == ActionName.Insert)
                {
                    if (_task.Insert(_view.Entity) > 0)
                    {
                        _view.Entity = null;
                        _view.SetMessage(Message.BLM_SUC_INSERT.ToString(), true);
                    }
                }
                else if (_view.Action == ActionName.Update)
                {
                    if (_task.Update(_view.Entity) > 0)
                    {
                        _view.SetMessage(Message.BLM_SUC_UPDATE.ToString(), true);
                    }
                }
            }
            catch (DataAccessException ex)
            {
                throw new BusinessLogicException(ex.Message);
            }
        }
    }
}